package com.example.designpatterns;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author code威
 * @version 1.0
 * @description: TODO
 * @date 2022/9/20 16:43
 */
@Component
public class HandlerFactory {
    private static Map<String,HandlerService> map = new ConcurrentHashMap<>();

    @Autowired
    public HandlerFactory(List<HandlerService> list){
        map.clear();
        for (HandlerService service : list) {
            map.put(service.getClass().getAnnotation(HandlerType.class).name(),service);
        }
    }


    public HandlerService getInstance(String typeName){
       return map.get(typeName);
    }
}
